package com.cykj.service.impl;

import com.cykj.dto.ResponseDTO;
import com.cykj.mapper.GoodMapper;
import com.cykj.service.IGoodService;
import com.cykj.vo.CyGoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author 王宇
 * @CreateTime: 2025-07-29  15:11
 * @BelongsProject: jd_seckill
 * @BelongsPackage: com.cykj.service.impl
 * @description 商品功能接口实现
 */
@Service
public class IGoodServiceImpl implements IGoodService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public ResponseDTO getGoodList() {
        // 获取所有缓存的商品信息对应的key
        Set<String> keys = redisTemplate.keys("good:*");
        if (keys == null || keys.isEmpty()) {
            return ResponseDTO.error("商品列表为空");
        }

        // 批量获取缓存的商品信息
        List<Object> goodList = redisTemplate.opsForValue().multiGet(keys);
        if (goodList != null && !goodList.isEmpty()) {
            return ResponseDTO.success("商品列表获取成功", goodList);
        }
        return ResponseDTO.error("商品列表为空");
    }

    @Override
    public ResponseDTO subGoodStock(CyGoodVO cyGoodVO) {
        Integer result = goodMapper.subGoodStock(cyGoodVO);
        if (result > 0) {
            return ResponseDTO.success("商品库存更新成功");
        }
        return ResponseDTO.error("商品库存更新失败");
    }
}
